Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Flatten an Abstract Syntax Tree by placing all the nodes in a single flat array.
For comments and suggestions feel free to open an issue or find me on Twitter - @ctrl__esc
npm install flast
Requires Node 16 or newer.
git clone git@github.com:PerimeterX/flast.git
cd flast
npm install
Input code: console.log('flAST');
.
Output object:
const tree = [
{
type: 'program',
start: 0,
end: 21,
range: [0, 21],
body: [
'<ref to nodeId#2>'
],
sourceType: 'script',
comments: [],
nodeId: 0,
src: "console.log('flAST');",
childNodes: [
'<ref to nodeId#1>'
],
parentNode: null,
scope: '<GlobalScope scopeId#0>'
},
{
type: 'ExpressionStatement',
start: 0,
end: 21,
range: [0, 21],
expression: '<ref to nodeId#2>',
nodeId: 1,
src: "console.log('flAST');",
childNodes: [
'<ref to nodeId#2>'
],
parentNode: '<ref to nodeId#0>',
scope: '<GlobalScope scopeId#0>'
},
{
type: 'CallExpression',
start: 0,
end: 20,
range: [0, 20],
callee: '<ref to nodeId#3>',
arguments: [
'<ref to nodeId#6>'
],
optional: false,
nodeId: 2,
src: "console.log('flAST')",
childNodes: [
'<ref to nodeId#3>',
'<ref to nodeId#6>'
],
parentNode: '<ref to nodeId#1>',
scope: '<GlobalScope scopeId#0>'
},
{
type: 'MemberExpression',
start: 0,
end: 11,
range: [0, 11],
object: '<ref to nodeId#4>',
property: '<ref to nodeId#5>',
computed: false,
optional: false,
nodeId: 3,
src: 'console.log',
childNodes: [
'<ref to nodeId#4>',
'<ref to nodeId#5>'
],
parentNode: '<ref to nodeId#2>',
scope: '<GlobalScope scopeId#0>'
},
{
type: 'Identifier',
start: 0,
end: 7,
range: [0, 7],
name: 'console',
nodeId: 4,
src: 'console',
childNodes: [],
parentNode: '<ref to nodeId#3>',
scope: '<GlobalScope scopeId#0>'
},
{
type: 'Identifier',
start: 8,
end: 11,
range: [8, 11],
name: 'log',
nodeId: 5,
src: 'log',
childNodes: [],
parentNode: '<ref to nodeId#3>',
scope: '<GlobalScope scopeId#0>'
},
{
type: 'Literal',
start: 12,
end: 19,
range: [12, 19],
value: "flAST",
raw: "'flAST'",
nodeId: 6,
src: "'flAST'",
childNodes: [],
parentNode: '<ref to nodeId#2>',
scope: '<GlobalScope scopeId#0>'
}
];
const {generateFlatAST, generateCode} = require('flast');
const ast = generateFlatAST(`console.log('flAST')`);
const reconstructedCode = generateCode(ast[0]); // rebuild from root node
const generateFlatASTDefaultOptions = {
detailed: true, // If false, include only original node without any further details
includeSrc: true, // If false, do not include node src. Only available when `detailed` option is true
};
See Espree's documentation for more information
const generateCodeDefaultOptions = {
format: {
indent: {
style: ' ',
adjustMultilineComment: true,
},
quotes: 'auto',
escapeless: true,
compact: false,
},
comment: true,
};
const {generateFlatAST, generateCode, Arborist} = require('flast');
const ast = generateFlatAST(`console.log('Hello' + ' ' + 'there!');`);
const replacements = {
'Hello': 'General',
'there!': 'Kenobi',
};
const arborist = new Arborist(ast);
// Mark all relevant nodes for replacement.
ast.filter(n => n.type === 'Literal' && replacements[n.value]).forEach(n => arborist.markNode(n, {
type: 'Literal',
value: replacements[n.value],
raw: `'${replacements[n.value]}'`,
}));
const numberOfChangesMade = arborist.applyChanges();
console.log(generateCode(arborist.ast[0])); // console.log('General' + ' ' + 'Kenobi');
The Arborist can be called with an extra argument - logFunc - which can be used to log inside the arborist.
To contribute to this project see our contribution guide
FAQs
Flatten JS AST
We found that flast demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.